package edu.fim.presentation.view;

import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import edu.fim.core.exception.ServiceException;
import edu.fim.core.service.LoginService;
import edu.fim.core.service.locator.ServiceLocator;

public class LoginForm extends AbstractForm {

	private static final long serialVersionUID = -5226475637443523307L;
	
	private JTextField userTextField;
	private JTextField passwordTextField;
	
	public LoginForm(String title) {
		super(title);
		initialize();
		update();
	}

	public void update() {
		resetFields();
		pack();
	}

	protected void initializeDynamicComponents() {
		userTextField = new JTextField();
		passwordTextField = new JPasswordField();
	}
	
	private void resetFields() {
		userTextField.setText("");
		passwordTextField.setText("");
	}

	protected void buildLayout() {
		Container container = new Container();
		container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
		container.add(createInputContainer());
		container.add(createButtonContainer());
		getContentPane().add(container);
	}
	
	private Container createInputContainer() {
		Container container = new Container();
		container.setLayout(new GridLayout(2, 2, 5, 5));
		container.add(new JLabel("User:"));
		container.add(userTextField);
		container.add(new JLabel("Password:"));
		container.add(passwordTextField);
		return container;
	}
	
	private Container createButtonContainer() {
		Container container = new Container();
		container.setLayout(new FlowLayout());
		Component loginComponent = createLoginComponent();
		Component exitComponent = createExitComponent();
		container.add(loginComponent);
		container.add(exitComponent);
		return container;
	}
	
	private Component createLoginComponent() {
		AbstractButton loginComponent = new JButton("Login");
		loginComponent.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				loginComponentActionPerformed();
			}
		});
		return loginComponent;
	}
	
	private Component createExitComponent() {
		AbstractButton exitComponent = new JButton("Exit");
		exitComponent.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				System.exit(0);
			}
		});
		return exitComponent;
	}
	
	private void loginComponentActionPerformed() {
		LoginService service = ServiceLocator.getImplementation(LoginService.class);
		try {
			service.login(userTextField.getText(), passwordTextField.getText());
		}
		catch (ServiceException e) {
			e.printStackTrace();
			showMessage(e.getMessage());
		}
		finally {
			resetFields();
		}
	}
}